package com.example.node.utils;

import android.os.Handler;
import com.example.node.listener.OnOkHttpCallback;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtils {

    public static final MediaType JSON
            = MediaType.get("application/json; charset=utf-8");

    private static OnOkHttpCallback Callback;

    private static Handler handler=new Handler();

    //get请求
    public static void get(String url,OnOkHttpCallback onOkHttpCallback){
        Callback=onOkHttpCallback;
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request=new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        //异步请求
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Callback.onFailure(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String result=response.body().string();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Callback.onSuccessful(result);
                        }
                    });
                }else {
                    Callback.onFailure("response not isSuccessful");
                }
            }
        });
    }

    //post json请求
    public static void postJson(String url,String json,OnOkHttpCallback onOkHttpCallback){
        Callback=onOkHttpCallback;
        OkHttpClient okHttpClient=new OkHttpClient();
        RequestBody body=RequestBody.create(JSON,json);
        Request request=new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Callback.onFailure(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String result=response.body().string();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Callback.onSuccessful(result);
                        }
                    });
                }else {
                    Callback.onFailure("response not isSuccessful");
                }
            }
        });
    }

    //post请求
    public static void postMap(String url, Map<String,String> params,OnOkHttpCallback onOkHttpCallback){
        Callback=onOkHttpCallback;
        OkHttpClient okHttpClient=new OkHttpClient();
        FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
        if(params!=null&&!params.isEmpty()){
            for(Map.Entry<String, String> entry:params.entrySet()){
                formBody.add(entry.getKey(),entry.getValue());
            }
        }
        FormBody body=formBody.build();
        Request request=new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Callback.onFailure(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String result=response.body().string();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Callback.onSuccessful(result);
                        }
                    });
                }else {
                    Callback.onFailure("response not isSuccessful");
                }
            }
        });
    }

    public static void download(){

    }
}
